/**
 * 简单的网络请求类
 */
class CurlUtil {




    public static curl(url: string, data: any = {}) {
        return new Promise(function (reslove, reject) {
            var request = new egret.HttpRequest();
            request.open(url, egret.HttpMethod.POST);
            //设置响应头
            request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            request.responseType = egret.HttpResponseType.TEXT;
            //请求成功
            request.addEventListener(egret.Event.COMPLETE, () => {
                //对所有的接口返回进行检查是否有任务完成或者成就达成
                let result = JSON.parse(request.response);
                reslove(result);
            }, this);
            //请求失败
            request.addEventListener(egret.IOErrorEvent.IO_ERROR, () => {
                reject();
            }, this);
            
            if (GameData.is_tiyan) {
                try {
                    const apptoken = utils.getCookie('apptoken');
                    if (apptoken && !data.hasOwnProperty('pre_apptoken')) {
                        data.pre_apptoken = apptoken;
                    }
                } catch (e) { }
            } else {
                try {
                    const tvmid = utils.getCookie('tvmid');
                    if (tvmid && !data.hasOwnProperty('tvmid')) {
                        data.tvmid = tvmid;
                    }
                } catch (e) { }
                try {
                    const apptoken = utils.getCookie('apptoken');
                    if (apptoken && !data.hasOwnProperty('apptoken')) {
                        data.apptoken = apptoken;
                    }
                } catch (e) { }
            }

            let str = '';
            for (var k in data) {
                if (str != '') {
                    str += '&';
                }
                str += k;
                str += '=';
                str += data[k];
            }
            request.send(str);
        });
    }
}
